<template>
	<view class="uni-container">
		<uni-forms ref="form" label-width="200rpx" :model="formData" validateTrigger="bind">
			<uni-forms-item name="is_enable" label="是否启用">
			  <switch @change="binddata('is_enable', $event.detail.value)" :checked="formData.is_enable"></switch>
			</uni-forms-item>
			<uni-forms-item name="appid" label="应用唯一标识">
				<uni-easyinput placeholder="应用唯一标识" v-model="formData.appid"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="appname" label="应用名称">
				<uni-easyinput placeholder="应用名称" v-model="formData.appname"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="index_banner" label="banner">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="banner"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="index_list" label="首页菜单">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="indexList"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="notice" label="公告">
				<uni-easyinput placeholder="公告内容" maxlength="-1" v-model="formData.notice"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="ads_seat" label="占位广告">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="ads_seat"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="ads_down" label="下载激励广告">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="ads_down"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="ads_inter" label="插屏广告">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="ads_inter"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="ads_task" label="任务激励广告">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="ads_task"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="download" label="下载地址">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="download"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="task_new" label="任务-新人">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="task_new"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="task_share" label="任务-分享">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="task_share"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="h5" label="h5">
				<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="h5"></uni-easyinput>
			</uni-forms-item>
      <uni-forms-item name="logs" label="统计日志">
      	<uni-easyinput placeholder="banner" type="textarea" maxlength="-1" autoHeight v-model="logs"></uni-easyinput>
      </uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>
<script>
	import edit from './edit.js';
	import { validator } from '@/js_sdk/validator/vm-config-list.js';
	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'vm-config-list';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}
	export default {
		mixins: [edit],
		data() {
			let formData = {
				"is_enable": null,
				"appid": "",
				"appname": "",
				"index_banner": [],
				"index_list": [],
				"notice": "",
				"ads_seat": null,
				"ads_down": null,
				"ads_inter": null,
				"ads_task": null,
				"download": null,
				"task_new": null,
				"task_share": null,
				"h5": null
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					const subData = JSON.parse(JSON.stringify(this.formData));
					delete subData._id;
					return this.submitForm(subData)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},
			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field("appid,appname,index_banner,index_list,notice,ads_seat,ads_down,ads_inter,ads_task,download,task_new,task_share,h5,is_enable").get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>